package com.kdzy.lshy.socity_aiom.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.io.File;

@RestController
@RequestMapping("/listenFileChanges")
@CrossOrigin("*")
public class ListenFileChangesController {

    private static final Logger logger = LoggerFactory.getLogger(ListenFileChangesController.class);

    /**
     * 全局查询 -- 根据级别查询
     * @return
     */
    @RequestMapping(value = "/listen", method = RequestMethod.GET)
    public Object listen() {
        try{
            Jedis redis = new Jedis("140.143.210.101", 6379);
            redis.auth("q1w2e3r4t5");
            String size1 = redis.get("size");
            if (size1 == null) {
                redis.set("size", "0");
                size1 = "0";
            }
            Integer sizeRedis = Integer.parseInt(size1); // 取出的初始值

            File file = new File("E:\\123");
            String[] list = file.list();
            int size = list.length; // 现在的文件个数

            if (size > sizeRedis ) { // 成功
                redis.set("size", size + "");
                return true;
            } else if (size == sizeRedis){
                return false;
            } else {
                redis.set("size", size + "");
                return false;
            }
        } catch (Exception e) {
            logger.error("异常信息：" + ExceptionDetailMessage.getExceptionDetail(e));
            return false;
        }
    }
}
